package ui;

import java.awt.Color;
import java.util.List;

public class StorageUi {

	private Frame frame;
	private javax.swing.JLabel label1;
	
	private class Frame extends javax.swing.JFrame{
		public void paint(java.awt.Graphics g) {
			super.paint(g);
			g.setColor(Color.BLUE);
			List<storage.RamTable.RamData> list=storage.RamTable.getRamDatas();
			int x,y,w;
			int size=0;
			for(storage.RamTable.RamData d:list){
				x=d.getStartLocation();
				y=d.getStartLocation();
				analyPaint(x,y,d.getLength(),g);
				size=size+d.getLength();
			}
			label1.setText("内存占有率："+size*100/512 + "%");
			g.fill3DRect(10,30,(size*100/512)*480/100,20,true);
			
		}
		
		private void analyPaint(int x,int y,int length,java.awt.Graphics g){
			int w;
			int start=x;
			int tempY=y;
			x=((x/128))*100+50;
			y=(y%128)+150;
			w=length;
			
			int top=(start/128+1)*128;
			if(top<start+length){
				w=top-start;
				analyPaint(top,top,length-w,g);
			}
			
			g.fill3DRect(x, y,20,w,true);
		}
		
	}
	
	public void start(){
		frame.setVisible(true);
	}
	
	public StorageUi(){
		frame=new Frame();
		frame.setSize(500,300);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setTitle("内存管理");
		
		label1=new javax.swing.JLabel();
		label1.setBounds(200,40,150,20);
		frame.setLayout(null);
		frame.add(label1);
		
		
		//frame.setVisible(true);
		
		T t=new T();
		t.setDaemon(true);
		t.start();
		
	}
	
	private class T extends Thread{
		
		public void run(){
			while(true){
				try{
					frame.repaint();
					Thread.sleep(1000);
				}catch(Exception error){}
			}
		}
		
	}
	
}
